from datetime import datetime, timezone, timedelta


def get_now_ts():
    """获取当前秒时间戳"""
    return int(datetime.now().timestamp())


def get_now_date(timezone_offset=8, utc=False):
    """获取当前时间对象"""
    if utc:
        return datetime.utcnow()
    local_timezone = timezone(timedelta(hours=timezone_offset))
    return datetime.now(local_timezone)


def ts_to_utc(ts: int):
    """将秒时间戳转为 utc 时间"""
    return datetime.fromtimestamp(ts, timezone.utc)


def ts_to_local(ts: int, timezone_offset=8):
    """将秒时间戳转换为用户本地时间（默认北京时间，UTC+8）"""
    utc_time = ts_to_utc(ts)
    local_timezone = timezone(timedelta(hours=timezone_offset))
    return utc_time.astimezone(local_timezone)


def get_offset_date(offset_seconds: int, timezone_offset=8, utc=False):
    """获取当前时间偏移指定秒数后的对象"""
    now = get_now_date(timezone_offset, utc)
    return now + timedelta(seconds=offset_seconds)


def get_seconds_timedelta(seconds: datetime):
    """获取指定秒数的时间差"""
    return timedelta(seconds=seconds)


def in_time_range(time_range: str, timezone_offset=8, utc=False) -> bool:
    """检查当前时间是否在给定的时间范围内"""
    try:
        start_str, end_str = time_range.split('-')

        start_time = datetime.strptime(start_str, "%H:%M:%S").time()
        end_time = datetime.strptime(end_str, "%H:%M:%S").time()

        now = get_now_date(timezone_offset, utc).time()

        # 处理跨午夜的情况（如 22:00-02:00）
        if end_time <= start_time:
            return now >= start_time or now <= end_time
        return start_time <= now <= end_time

    except Exception:
        return False


if __name__ == '__main__':
    now_ts = get_now_ts()
    print(ts_to_local(now_ts))
    print(ts_to_utc(now_ts))
    print(get_now_date(utc=True))
    print(get_now_date())
    print(in_time_range("00:00:00-23:59:59"))
    print(in_time_range("22:00:00-03:13:40"))
